<template>
  <el-dialog :visible.sync="dialog" :close-on-click-modal="false" :before-close="cancel" :title="'配置主要信息'"
             append-to-body width="820px">
    <el-form ref="form" :inline="true" :model="form" :rules="rules" size="small" label-width="80px">
      <el-form-item label="包名" prop="packageName">
        <el-input v-model="form.packageName" style="width: 245px;"/>
      </el-form-item>
      <el-form-item label="模块名" prop="moduleName">
        <el-input v-model="form.moduleName" style="width: 245px;"/>
      </el-form-item>
      <el-form-item label="根类名" prop="baseClassName">
        <el-input v-model="form.baseClassName" style="width: 245px;"/>
      </el-form-item>
      <el-form-item label="作者" prop="author">
        <el-input v-model="form.author" style="width: 245px;"/>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email" style="width: 245px;"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
    import {
        queryGeneratorConfigByCondition,
        updateGeneratorConfig,
        saveGeneratorConfigSingleLineData
    } from '@/api/GeneratorConfig'

    export default {
        components: {},
        data() {
            return {
                loading: false, dialog: false,
                form: {
                    id: '',
                    configType: '',
                    packageName: '',
                    moduleName: '',
                    author: '',
                    email: '',
                    baseClassName: ''
                },
                rules: {
                    moduleName: [
                        {required: true, message: '请输入模块名', trigger: 'blur'}
                    ],
                    package: [
                        {required: true, message: '请输入包名', trigger: 'blur'}
                    ],
                    baseClassName: [
                        {required: true, message: '请输入包名', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            cancel() {
                this.resetForm()
            },
            doSubmit() {
                let data = this.form;
                let id = data.id;
                console.log(id);
                if (id) {
                    console.log(this.form.configType)
                    this.doUpdate(data);
                } else {
                    console.log(this.form.configType)
                    this.doSave(data);
                }
            },
            doUpdate(data) {
                updateGeneratorConfig(data).then(res => {
                    this.doQueryGeneratorCodeConfig(data);
                    if (res.hasError) {
                        this.$notify({
                            title: res.error.message,
                            type: 'fail',
                            duration: 2500
                        })
                    } else {
                        this.$notify({
                            title: res.data,
                            type: 'success',
                            duration: 2500
                        })
                    }
                }).catch(err => {
                    this.loading = false
                    console.log(err.response.data.message)
                })
            },
            doSave(data) {
                saveGeneratorConfigSingleLineData(data).then(res => {
                    this.doQueryGeneratorCodeConfig(data);
                    if (res.hasError) {
                        this.$notify({
                            title: res.error.message,
                            type: 'fail',
                            duration: 2500
                        })
                    } else {
                        this.$notify({
                            title: res.data,
                            type: 'success',
                            duration: 2500
                        })
                    }
                }).catch(err => {
                    this.loading = false
                    console.log(err.response.data.message)
                })
            },
            resetForm() {
                this.dialog = false
                this.$refs['form'].resetFields()
            },
            doQueryGeneratorCodeConfig(data) {
                queryGeneratorConfigByCondition(data).then(res => {
                    if (res.hasError) {
                        this.$notify({
                            title: res.error.message,
                            type: 'fail',
                            duration: 2500
                        })
                    } else {
                        if (res.data == '') {
                            res.data = {}
                        }
                        this.form = res.data;
                    }
                    this.form.configType = data.configType;
                    console.log(this.form.configType)

                }).catch(err => {
                    this.loading = false
                    console.log(err.response.data.message)
                })

            }
        }
    }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  /deep/ .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
